<?php

define('IN_DOUCO', true);

require (dirname(__FILE__) . '/include/init.php');
// rec操作项的初始化
$rec = $check->is_rec($_REQUEST['rec']) ? $_REQUEST['rec'] : 'default';
//所有主播
$anchors = $dou->get_all("SELECT * FROM ".$dou->table('live_anchor')." ORDER BY id DESC");
$smarty->assign('anchors', $anchors);
$smarty->assign('anchors_json', json_encode($anchors));

$smarty->assign('rec', $rec);
$smarty->assign('cur', 'chatroom');
$smarty->assign('ur_here', $_LANG['chatroom']);
$smarty->assign('chatroom_link', array ('text' => $_LANG['room_manager'],'href' => 'chatroom.php')); 

//列表
if ($rec == 'default') {
    $name = i('name','','request');
    $where = " WHERE 1 ";
    if($name){
        $where .= " AND name LIKE '%$name%' ";
        $get = "&name=$name";
    }
    //分页
    $page = $check->is_number($_REQUEST['page']) ? $_REQUEST['page'] : 1;
    $page_url = 'chatroom.php?1=1';
    $sql = "SELECT * FROM ".$dou->table('chatroom')." $where ORDER BY id DESC";
    $limit = $dou->pager_sql($sql, 15, $page, $page_url, $get);
    $res = $dou->get_all($sql.$limit);
    foreach ($res as $k=>$row) {
        $chatroom_list[$k] = $row;
        $chatroom_list[$k]['content']   = htmlspecialchars_decode($row['content']);//strip_tags()
        $chatroom_list[$k]['chat_time'] = date("Y-m-d H:i:s", $row['chat_time']);
    }
    // 赋值给模板
    $smarty->assign('chatroom_list', $chatroom_list);
    $smarty->assign('ur_here', $_LANG['room_manager']);
    $smarty->display('chatroom.htm');
}

//新增
elseif ($rec == 'add') {
    $smarty->assign('ur_here', $_LANG['room_manager']);
    $smarty->assign('action_link', array ('text' => $_LANG['room_manager'],'href' => 'chatroom.php' ));
    // CSRF防御令牌生成
    $smarty->assign('token', $firewall->get_token());
    $smarty->assign('chatroom', $chatroom);
    $smarty->display('chatroom.htm');
} 
elseif ($rec == 'insert') {
    if(empty($_POST['name'])) $dou->dou_msg($_LANG['room_name_empty']);
    if(empty($_POST['roomnumber'])) $dou->dou_msg($_LANG['roomnumber_empty']);
    // CSRF防御令牌验证
    $firewall->check_token($_POST['token']);
    $_data = array('name'=>$_POST['name'],
        'roomnumber'=>$_POST['roomnumber'],
        'remark'=>$_POST['remark'],
        'status'=>$_POST['status'],
        'weigh'=>$_POST['weigh'],
    );
    //其他模块数据
    $mode_data = common_data();
    $_data['kf'] = $mode_data['kf'];
    $_data['yc'] = $mode_data['yc'];
    //$_data['live'] = $mode_data['live'];
    //图片上传
    $img = upload_image('img','chatroom');
    if(!empty($img)) $_data['img'] = $img;
    $dou->query($dou->insert_sql('chatroom', $_data));
    //live_time_data($dou->insert_id());

    //操作记录
    $log_msg = $_LANG['room_add'].': '.$_POST['name'];
    $dou->create_admin_log($log_msg);
    $dou->dou_msg($_LANG['edit_succes'], 'chatroom.php');
}

//编辑
elseif ($rec == 'edit') {
    $smarty->assign('ur_here', $_LANG['room_manager']);
    $smarty->assign('action_link', array ('text' => $_LANG['room_manager'],'href' => 'chatroom.php' ));
    // 验证并获取合法的ID
    $id = $check->is_number($_REQUEST['id']) ? $_REQUEST['id'] : '';
    $chatroom = $dou->get_row("SELECT * FROM ".$dou->table('chatroom')." WHERE id='$id' ");
    $chatroom['kf'] = unserialize($chatroom['kf']);
    $chatroom['yc'] = unserialize($chatroom['yc']);
    $chatroom['live'] = $dou->get_all("SELECT * FROM ".$dou->table('live_time')." WHERE room_id='$id'");
    // CSRF防御令牌生成
    $smarty->assign('token', $firewall->get_token());
    $smarty->assign('chatroom', $chatroom);
    $smarty->display('chatroom.htm');
} 
elseif ($rec == 'update') {
    if(empty($_POST['name'])) $dou->dou_msg($_LANG['room_name_empty']);
    if(empty($_POST['roomnumber'])) $dou->dou_msg($_LANG['roomnumber_empty']);
    // CSRF防御令牌验证
    $firewall->check_token($_POST['token']);
    $_data = array('name'=>$_POST['name'],
        'roomnumber'=>$_POST['roomnumber'],
        //'img'=>$_POST['img'],
        'remark'=>$_POST['remark'],
        'status'=>$_POST['status'],
        'weigh'=>$_POST['weigh'],
    );
    //其他模块数据
    $mode_data = common_data();
    $_data['kf'] = $mode_data['kf'];
    $_data['yc'] = $mode_data['yc'];
    //$_data['live'] = $mode_data['live'];
    //live_time_data($_POST['id']);
    //图片上传
    $img = upload_image('img','chatroom');
    if(!empty($img)) $_data['img'] = $img;
    $dou->query($dou->update_sql('chatroom', $_data, "id='{$_POST['id']}'"));
    //操作记录
    $log_msg = $_LANG['room_edit'].': '.$_POST['name'];
    $dou->create_admin_log($log_msg);
    $dou->dou_msg($_LANG['edit_succes'], 'chatroom.php');
} 

//删除
elseif ($rec == 'del') {
    $id = $check->is_number($_REQUEST['id']) ? $_REQUEST['id'] : $dou->dou_msg($_LANG['illegal'], 'chatroom.php');
    $name = $dou->get_one("SELECT name FROM " . $dou->table('chatroom') . " WHERE id = '$id'");
    if (isset($_POST['confirm']) ? $_POST['confirm'] : '') {
        $dou->create_admin_log($_LANG['chatroom_del'] . ': ' . $name);
        $dou->query($dou->delete_sql('live_time', "room_id='$id'" )); 
        $dou->delete($dou->table('chatroom'), "id = $id", 'chatroom.php');
    } else {
        $_LANG['del_check'] = preg_replace('/d%/Ums', $name, $_LANG['del_check']);
        $dou->dou_msg($_LANG['del_check'], 'chatroom.php', '', '30', "chatroom.php?rec=del&id=$id");
    }
}

//批量操作选择
elseif ($rec == 'action') {
    if (is_array($_POST['checkbox'])) {
        if ($_POST['action'] == 'del_all') {//批量删除
            $dou->query($dou->delete_sql('live_time', "room_id ".$dou->create_sql_in($_POST['checkbox']) )); 
            $dou->del_all('chatroom', $_POST['checkbox'], 'id');
        } else {
            $dou->dou_msg($_LANG['select_empty']);
        }
    } else {
        $dou->dou_msg($_LANG['record_select_empty']);
    }
}

/**
 * +----------------------------------------------------------
 * 其他设置
 * +----------------------------------------------------------
 */
elseif ($rec == 'config') {

    if(isset($_POST['submit'])){
        $skins_arr = array();
        //网站logo
        $web_logo = upload_image('web_logo','web_config/web_logo');
        if($web_logo) $dou->query($dou->update_sql('config', array('value'=>$web_logo), "name='web_logo'") );
        //pc皮肤
        $pc_skins = upload_multi_image('pc_skins','web_config/pc_skins', true, '100','60');
        if($pc_skins){
            foreach($pc_skins as $k=>$v){ 
                if(!empty($v)){
                    $_POST['pc_skins_val'][$k] = $v;
                } 
                $skins_arr[$k]['img']   = $_POST['pc_skins_val'][$k]; 
                $skins_arr[$k]['thumb'] = thumb_path($_POST['pc_skins_val'][$k]);
            }
            $dou->query($dou->update_sql('config', array('value'=>serialize($skins_arr)), "name='pc_skins'"));
        }
        //h5皮肤
        $h5_skins = upload_multi_image('h5_skins','web_config/h5_skins');
        if($h5_skins){
            foreach($h5_skins as $k=>$v){ 
                if(!empty($v)){
                    $_POST['h5_skins_val'][$k] = $v;
                } 
            }
            $dou->query($dou->update_sql('config', array('value'=>serialize($_POST['h5_skins_val'])), "name='h5_skins'"));
        }
        $allow_upload_group = is_array($_POST['allow_upload_group']) ? $_POST['allow_upload_group'] : array();
        $dou->query($dou->update_sql('config', array('value'=>$_POST['allow_visitor_say']), "name='allow_visitor_say'") );
        $dou->query($dou->update_sql('config', array('value'=>serialize($allow_upload_group) ), "name='allow_upload_group'") );
        $dou->query($dou->update_sql('config', array('value'=>$_POST['say_filter']), "name='say_filter'") );
        $dou->query($dou->update_sql('config', array('value'=>$_POST['ip_filter']), "name='ip_filter'") );
        $dou->query($dou->update_sql('config', array('value'=>$_POST['blacklist_filter']), "name='blacklist_filter'") );
        $dou->dou_clear_cache(ROOT_PATH . 'cache');
        $dou->dou_msg($_LANG['edit_succes'], 'chatroom.php?rec=config');
    }

    $smarty->assign('web_logo', $dou->get_config('web_logo'));
    $smarty->assign('pc_skins', unserialize($dou->get_config('pc_skins')));
    $smarty->assign('h5_skins', unserialize($dou->get_config('h5_skins')));

    $smarty->assign('allow_visitor_say', $dou->get_config('allow_visitor_say'));
    $smarty->assign('allow_upload_group', unserialize($dou->get_config('allow_upload_group')));

    $smarty->assign('say_filter', $dou->get_config('say_filter'));
    $smarty->assign('ip_filter', $dou->get_config('ip_filter'));
    $smarty->assign('blacklist_filter', $dou->get_config('blacklist_filter'));
    $smarty->display('chatroom.htm');
}



function common_data(){
    global $dou;
    $_data['kf'] = $_data['yc'] = $_data['live'] = '';
    //客服
    if($_POST['kf_name']){
        foreach($_POST['kf_name'] as $k=>$v){
            if(!empty($_POST['kf_name'][$k]) && !empty($_POST['kf_qq'][$k])) 
                $kfs[] = array('kf_name'=>$_POST['kf_name'][$k], 'kf_qq'=>$_POST['kf_qq'][$k], 'kf_key'=>$_POST['kf_key'][$k]);
        }
        $_data['kf'] = serialize($kfs);
    }
    //预测模块
    if($_POST['yc_mode']){
        foreach($_POST['yc_mode'] as $k=>$v){
            if(!empty($_POST['yc_mode'][$k]) && !empty($_POST['yc_name'][$k])) 
                $ycs[] = array('yc_mode'=>$_POST['yc_mode'][$k], 'yc_name'=>$_POST['yc_name'][$k], 'yc_url'=>$_POST['yc_url'][$k]);
        }
        $_data['yc'] = serialize($ycs);
    }
    //直播
    /*if($_POST['anchor_id']){
        foreach($_POST['anchor_id'] as $k=>$v){
            if(!empty($_POST['anchor_id'][$k]) && !empty($_POST['live_start'][$k])) 
                $live[] = array('anchor_id'=>$_POST['anchor_id'][$k], 'live_start'=>$_POST['live_start'][$k], 'live_end'=>$_POST['live_end'][$k]);
        }
        $_data['live'] = serialize($live);
    }*/
    return $_data;
}
function live_time_data($room_id){
    global $dou;
    $insert_room_id = $room_id;
    if($_POST['anchor_id']){
        //删除旧记录
        $dou->query($dou->delete_sql('live_time', "room_id='$insert_room_id'")); 
        //新增记录
        foreach($_POST['anchor_id'] as $k=>$v){
            if(!empty($_POST['anchor_id'][$k]) && !empty($_POST['live_start'][$k])) {
                $_data = array('room_id'=>$insert_room_id, 
                    'anchor_id'   => $_POST['anchor_id'][$k], 
                    'live_start'  => $_POST['live_start'][$k], 
                    'live_end'    => $_POST['live_end'][$k],
                    'create_time' => time()
                );
                $dou->query($dou->insert_sql('live_time', $_data));
            }
        }
    }
}